//Bob = new tweetSearcher();
Fred = '';
userSearch = 0;

function tweetSearcher(){
	this.pageLimit = 16;
	this.currentPage = 0;
	this.geoCount = 0;
	this.term = ' ';
	userSearch = 0;
	
	this.geo = new Array(1000);
	for(var c = 0; c < 1000; c++)
		this.geo[c] = new coord();
	
	this.getGeo = getGeo;
	this.setTerm = setTerm;
	//this.fetchTweets = fetchTweets;
	this.search = search;
	this._search = _search;
	this.print = print;
	this.setPageLimit = setPageLimit;
	this.getPageLimit = getPageLimit;
	this.setCurrentPage = setCurrentPage;
	this.getCurrentPage = getCurrentPage;
}

function getGeo(index){
	return this.geo[index];
}

function setTerm(newTerm){
	this.term = newTerm;
}

function setPageLimit(newLimit){
	this.pageLimit = newLimit;
}

function getPageLimit(){
	return this.pageLimit;
}

function setCurrentPage(newPage){
	this.currentPage = newPage;
}

function getCurrentPage(){
	return this.currentPage;
}

function getLocations(location)
{
	
	//var script3 = document.createElement("script");
	//script3.src += 'http://where.yahooapis.com/geocode?location='+location+'&flags=J&appid=piSn0w74';
	//document.body.appendChild(script3);
	$.getJSON('http://where.yahooapis.com/geocode?location='+location+'&flags=J&appid=piSn0w74',function(data)
		{
			//var i = data.ResultSet.Results.length;
			if(data.ResultSet.Found > 0){
				//document.getElementById("test").innerHTML+='found user'+'<br/>';
				Bob.geo[Bob.geoCount].setLat(data.ResultSet.Results[0].latitude);
				Bob.geo[Bob.geoCount].setLon(data.ResultSet.Results[0].longitude);
				Bob.geoCount += 1;
			}
		}
	);
}

function fetchUsers(data)
{
	var i = data.length;
	//document.getElementById("test").innerHTML+=i+'<br/>';
	while(i--)
	{
		user = data[i];
		if(user.location != null){
			getLocations(user.location);
			//piSn0w74
		}
	
	}
}


function fetchTweets(data)
{
	Fred = '';
	var i = data.results.length;
	//document.getElementById("test").innerHTML+=i+'<br/>';
	while (i--) {
		tweet = data.results[i];
		//document.getElementById("test").innerHTML+=tweet.text+'<br/>';
		if(tweet.geo != null){
			//document.getElementById("test").innerHTML+=tweet.geo.coordinates[0]+' '+tweet.geo.coordinates[1]+'<br/>';
			Bob.geo[Bob.geoCount].setLat(tweet.geo.coordinates[0]);
			Bob.geo[Bob.geoCount].setLon(tweet.geo.coordinates[1]);
			Bob.geoCount += 1;
		}
		else{
			//if(i > 75)
				Fred += tweet.from_user_id+',';
		}
	}
	Fred += '0000';
	if(userSearch == 0)
	{
		var script2 = document.createElement("script");
		script2.src = 'https://api.twitter.com/1/users/lookup.json?user_id='+Fred+'&include_entities=true&callback=fetchUsers';
		document.body.appendChild(script2);
		userSearch = 1;
	}
}

function _search()
{
	//document.getElementById("test").innerHTML+=this.currentPage+' '+this.term;
	var script = document.createElement("script");
	this.setCurrentPage(this.getCurrentPage()+1);
	script.src = 'http://search.twitter.com/search.json?q='+this.term+'&page='+this.currentPage+'&rpp=100&callback=fetchTweets';
	document.body.appendChild(script);
}

function search()
{
	//Two possible implementations here, work equally well 
	
	//while(this.getCurrentPage() <= this.getPageLimit()){
	//	this._search();
	//}
	
	for(var p = 0; p <= this.getPageLimit(); p++)
		this._search();
	
}

function print()
{
	var i;
	for(i = 0; i < 100; i++)
		document.getElementById("test").innerHTML+=this.geo[i].getLat()+' '+this.geo[i].getLon()+'<br />';
}



//                               0xxx{:::::> Sample Implementation

function main(searchTerm)
{
	Bob = new tweetSearcher();  //Keep the instance global to store coordinates between searches, we keep it ghetto like that
	Bob.setTerm(searchTerm);	//Set the search term to your input, self explanatory really
	Bob.search();				//Run the search, this will populate the geo array or coord objects
								//If no coordinates are returned, you got rate limited, son.
	getLocations("San Francisco, CA");
}

function Bprint(){
	Bob.print();
}
